/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.admin.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import com.yami.shop.bean.enums.UploadType;
import com.yami.shop.common.bean.Qiniu;
import com.yami.shop.common.response.ResponseEnum;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.ImgUploadUtil;
import com.yami.shop.service.AttachFileService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.Objects;

/**
 * 文件上传 controller
 * @author lgh
 *
 */
@RestController
@RequestMapping("/admin/file")
@Slf4j
public class FileController {

	@Autowired
	private AttachFileService attachFileService;
    @Autowired(required = false)
	private Qiniu qiniu;
	@Autowired
	private ImgUploadUtil imgUploadUtil;

    /**
     * x-file-storage
     */
    @Autowired
    private FileStorageService fileStorageService;//注入实列


    @PostMapping("/upload")
    public ServerResponseEntity uploadFile(MultipartFile file) throws Exception {
        // 检查文件是否为空
        if (file.isEmpty()) {
            return ServerResponseEntity.success("文件为空");
        }

        // 获取当前日期并格式化为年/月/日形式
        String datePath = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy/MM/dd"));

        // 获取文件扩展名
        String extName = FileUtil.extName(file.getOriginalFilename());

        // 上传文件到OSS服务
        log.info("开始上传文件，文件信息: {}", file);
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath(datePath.replace("/", "/")) // 设置存储路径（仅日期部分）
                .setName(file.getOriginalFilename()) // 使用原始文件名上传
                .upload();

        // 获取OSS返回的完整文件路径
        String ossFullPath = fileInfo.getUrl(); // 假设FileInfo类提供了getFullPath()方法

        // 构建保存到数据库的地址
        String databasePath = ossFullPath;

        // 日志输出保存到数据库的路径
        log.info("保存到数据库的文件路径: {}", databasePath);

        // 返回保存到数据库的路径
        return ServerResponseEntity.success(databasePath);
    }
    @PostMapping("/upload/element")
	public ServerResponseEntity<String> uploadElementFile(@RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ServerResponseEntity.success();
        }
		String fileName = attachFileService.uploadFile(file);
        return ServerResponseEntity.success(fileName);
	}

	@PostMapping("/upload/tinymceEditor")
	public ServerResponseEntity<String> uploadTinymceEditorImages(@RequestParam("editorFile") MultipartFile editorFile) throws IOException{
		String fileName =  attachFileService.uploadFile(editorFile);
		String data = "";
		if (Objects.equals(imgUploadUtil.getUploadType(), UploadType.LOCAL.value())) {
			data = imgUploadUtil.getUploadPath() + fileName;
		} else if (Objects.equals(imgUploadUtil.getUploadType(), UploadType.QINIU.value())) {
			data = qiniu.getResourcesUrl() + fileName;
		}
        return ServerResponseEntity.success(data);
	}



}
